package com.my.servlet;

import com.alibaba.fastjson.JSON;
import com.my.controal.JDBC;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

@WebServlet("/Login")
public class Login extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 处理中文乱码
        response.setContentType("application/json;charset=utf-8");
        response.setCharacterEncoding("UTF-8");

        String userID = request.getParameter("userID");

        String sql = String.format("select tb_user.userID, tb_user.password from tb_user where tb_user.userID = '%s'", userID);

        // 存放从sql语句查询的结果
        ArrayList<String> array = new ArrayList<String>();

        try {
            ResultSet res = JDBC.Instance().selectTable(sql);

            if (res.next()) {
                array.add("1");
                array.add(res.getString("userID"));
                array.add(res.getString("password"));
            }
            else {
                array.add("0");
            }

            String str = JSON.toJSONString(array);
            response.getWriter().print(str);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
